Helpful Information
 
 
Category: Javascript
JavaScript -> Long number sufix

Hi, I'm new here on this forum and I have registered to receive help in certain situations and help others members if I can.
I start with a problem which gives me a headache, I created a prototype function that adds a multiple or submultiple metric string, works in if..if..if case but if I try to compact the code in a shorten way I get an error .
In this case 1e+5 => 100,000, with an alert: alert (1e+5) works perfectly, but when I try this code does not work at all:


var pw = 5;
alert (1e + (pw));

The complete code is at the end, I want to mention that the code below in comment (if...if...if) it works great but is very bad to read and to edit something, so I try to use a loop.
If anyone has a better solution please post it.
Thank you very much, I owe you one!



var ESE = {};
ESE.Component = function()
{
/*
...
...
...
*/
}
ESE.prototype.getPosSufix=function(value)
{
var sufix="";
var rval=value;
var i,j=0;
var sTable=
[
/*Metric multiples*/
"yotta","zetta","exa","peta","tera",
"giga","mega","kilo","hecto","deca",
/*Metric submultiples*/
"deci","centi","mili","micro","nano",
"pico","femto","atto","zepto","yocto"
];
for(i=24;i>=1;i=i-3)
{
/*if(value>=(1e+(i))&&value<(10e+(i+2)))
{
if(i==3 && j==8) {i+=3}
rval=value*(1e-(i));
sufix=sTable[j];
}*/
alert(1e+0+i);
j++;
}
/*if(value>=1e-24&&value<10e-22)
{
rval=value*1e+24;
sufix="yocto";
}
if(value>=1e-21&&value<10e-19)
{
rval=value*1e+21;
sufix="zepto";
}
if(value>=1e-18&&value<10e-16)
{
rval=value*1e+18;
sufix="atto";
}
if(value>=1e-15&&value<10e-13)
{
rval=value*1e+15;
sufix="femto";
}
if(value>=1e-12&&value<10e-10)
{
rval=value*1e+12;
sufix="pico";
}
if(value>=1e-9&&value<10e-7)
{
rval=value*1e+9;
sufix="nano";
}
if(value>=1e-6&&value<10e-4)
{
rval=value*1e+6;
sufix="micro";
}
if(value>=1e-3&&value<10e-1)
{
rval=value*1e+3;
sufix="mili";
}
if(value>=1e-2&&value<10e-1)
{
rval=value*1e+2;
sufix="centi";
}
if(value>=1e-1&&value<10e-1)
{
rval=value*1e+1;
sufix="deci";
}
if(value>=1e+1&&value<10e+1)
{
rval=value*1e-1;
sufix="deca";
}
if(value>=1e+2&&value<10e+2)
{
rval=value*1e-2;
sufix="hecto";
}
if(value>=1e+3&&value<10e+5)
{
rval=value*1e-3;
sufix="kilo";
}
if(value>=1e+6&&value<10e+8)
{
rval=value*1e-6;
sufix="mega";
}
if(value>=1e+9&&value<10e+11)
{
rval=value*1e-9;
sufix="giga";
}
if(value>=1e+12&&value<10e+14)
{
rval=value*1e-12;
sufix="tera";
}
if(value>=1e+15&&value<10e+17)
{
rval=value*1e-15;
sufix="peta";
}
if(value>=1e+18&&value<10e+20)
{
rval=value*1e-18;
sufix="exa";
}
if(value>=1e+21&&value<10e+23)
{
rval=value*1e-21;
sufix="zetta";
}
if(value>=1e+24&&value<10e+26)
{
rval=value*1e-24;
sufix="yotta";
}*/
return [rval,sufix];
}

Try this:-


<script type = "text/javascript">

var pw = "5"; // a string value, not a number
var x = "1e" + pw; // concatenate strings
x = Number(x); // make a number
alert (x); // 100000
x = x.toExponential();
alert (x); // 1e+5

</script>


Quizmaster: A monocracy is a form of government in which how many people rule?
Contestant: Twelve.

Number object do the trick, I did not thought about this solution (to convert to a number object), because the type of 1e+24 is treated as a number, but simply 1e does not mean anything.
Thank you for fast-reply and for explanations!

Another alternative would be to use parseFloat.


var pw = 5;
alert (parseFloat('1e' + (pw)));

Again thanks a lot for your help!
There is the final result:


function getPrefix(value)
{
var prefix="";
var retVal = value;

var i, j=0;
var min, max, mul;

// Table ordered by smallest submultiple to highest multiple
var sTable=
[
// Metric submultiples
"yocto","zepto","atto","femto","pico",
"nano","micro","mili","centi","deci",
// Metric multiples
"deca","hecto","kilo","mega","giga",
"tera","peta","exa","zetta","yotta"
];

for(i=-24; i<=24; i+=3)
{
min="1e"+(i.toString());
min=Number(min);

max="1e"+((i+3).toString());
max=Number(max);

if(i==0)
i+=1;
if(i>=0)
mul="1e-"+(Math.abs(i).toString());
else
mul="1e"+(Math.abs(i).toString());
mul=Number(mul);

if(value >= min && value < max)
{
prefix = sTable[j];
retVal = value * mul;
document.write("_____PICKED_____<br />");
document.write("[I: "+i+"]<br />X: "+min+" -> Y: "+max+" -> Z: "+mul+"<br />");
break;
}
else if(prefix == "" && i == 24)
prefix = "prototype prefix";

document.write("[I: "+i+"]<br />X: "+min+" -> Y: "+max+" -> Z: "+mul+"<br />");

if(i >= -3 && i < 0 || i > 0 && i < 3)
i -= 2;
j++;
}
return [retVal,prefix];
}

var gp=getPrefix(0.1);
document.write("Value: " + gp[0] + " " + gp[1] + "<br />");

Great, but you should get rid of document.write() which is obsolete. Remember that any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it).










privacy (GDPR)